﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using System;
using System.Data;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_Dic_List : BaseForm
    {
        BLL.ServiceBase service = new BLL.ServiceBase();
        public Base_Dic_List()
        {
            InitializeComponent();
            BindDicTypeData();
        }

        private void BindDicTypeData()
        {
            dgDicType.DataSource = service.GetDicMain();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            dgDicValue.DataSource = null;
            BindDicValue();
        }

        private void BindDicValue()
        {
            strDicName = string.Empty;
            DataTable dt = service.GetDicValueByTableName(GetDicMainTableName());
            dgDicValue.DataSource = dt;
        }

        private string GetDicMainTableName()
        {
            GridView rowsSelected = gridView1 as GridView;
            int[] inteurm = rowsSelected.GetSelectedRows();
            string tableName = "";
            if (inteurm != null && inteurm.Length > 0)
            {
                tableName = rowsSelected.Rows[inteurm[0]].DataRow["DicTableName"].ToString();
            }
            return tableName;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string strTableName = GetDicMainTableName();
            if (strTableName.ToSafeString().Length == 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择字典类别！");
                return;
            }
            Base_Dic_Option option = new Base_Dic_Option(strTableName, string.Empty);
            if (option.ShowDialog() == DialogResult.OK)
            {
                BindDicValue();
            }
            option.Dispose();
        }

        private void gridView2_DoubleClick(object sender, EventArgs e)
        {
            if (gridView2.GetFocusedValue() != null)
            {
                Base_Dic_Option option = new Base_Dic_Option(GetDicMainTableName(), GetDicNO());
                if (option.ShowDialog() == DialogResult.OK)
                {
                    BindDicValue();
                }
                option.Dispose();
            }
        }
        string strDicName = string.Empty;
        private string GetDicNO()
        {
            string strDicNo = string.Empty;
            GridView rows = gridView2 as GridView;
            int[] inteurm = rows.GetSelectedRows();
            if (inteurm != null && inteurm.Length > 0)
            {
                strDicNo = rows.Rows[inteurm[0]].DataRow["DicNO"].ToString();
                strDicName = rows.Rows[inteurm[0]].DataRow["DicName"].ToString();
            }
            return strDicNo;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("确定要把该数据改成无效吗？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                if (service.SaveDicValue(GetDicMainTableName(), GetDicNO(), strDicName, "0"))
                {
                    BindDicValue();
                }
            }
        }
    }
}
